package com.wnhz.work.yz.adapter;

import android.content.Context;
import android.support.v7.app.AlertDialog;
import android.text.Selection;
import android.text.Spannable;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.wnhz.work.yz.MyApplication;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.bean.LookDetailsBean;
import com.wnhz.work.yz.bean.LookDetialNumsBean;
import com.wnhz.work.yz.constant.Constants;
import com.wnhz.work.yz.utils.BroadCastReceiverUtil;

import org.greenrobot.eventbus.EventBus;
import org.xutils.x;

import java.util.List;

/**
 * Created by admin on 2017/3/21.
 */
public class AllLookDetialsAdapter extends BaseAdapter {
    private Context context;
    private List<LookDetailsBean.InfoEntity.CateInfoEntity.DataEntity> dataList;
    private AllocationViewHolder viewHolder;

    public AllLookDetialsAdapter(Context context, List<LookDetailsBean.InfoEntity.CateInfoEntity.DataEntity> dataList) {
        this.context = context;
        this.dataList = dataList;
    }

    @Override
    public int getCount() {
        return dataList.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    //private Toast toast;

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        viewHolder = null;
        if (convertView == null) {
            viewHolder = new AllocationViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.all_look_content_item, null);
            viewHolder.good_logo = (ImageView) convertView.findViewById(R.id.good_logo);
            viewHolder.img_toast = (ImageView) convertView.findViewById(R.id.img_toast);
            viewHolder.reduce = (ImageView) convertView.findViewById(R.id.reduce);
            viewHolder.ll_add = (ImageView) convertView.findViewById(R.id.ll_add);
            viewHolder.text_title = (TextView) convertView.findViewById(R.id.text_title);
            viewHolder.text_price = (TextView) convertView.findViewById(R.id.text_price);
            viewHolder.text_desc = (TextView) convertView.findViewById(R.id.text_desc);
            viewHolder.shopping_number = (TextView) convertView.findViewById(R.id.shopping_number);
            viewHolder.is_select = (LinearLayout) convertView.findViewById(R.id.is_select);
            viewHolder.text1 = (TextView) convertView.findViewById(R.id.msg1);
            viewHolder.text2 = (TextView) convertView.findViewById(R.id.msg2);
            viewHolder.text3 = (TextView) convertView.findViewById(R.id.msg3);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (AllocationViewHolder) convertView.getTag();
        }
        viewHolder.img_toast.setVisibility(View.GONE);
        viewHolder.img_toast.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for (int i = 0; i < dataList.size(); i++) {
                    if (i == position) {
                        dataList.get(position).setSelect(!dataList.get(position).isSelect());
                    } else {
                        dataList.get(position).setSelect(false);
                    }
                }
                notifyDataSetChanged();
            }
        });
        try {
            viewHolder.text1.setText(dataList.get(position).getOther().get(0));
            viewHolder.text2.setText(dataList.get(position).getOther().get(1));
            viewHolder.text3.setText(dataList.get(position).getOther().get(2));
        } catch (Exception e) {

        }

        if (dataList.get(position).isSelect()) {
            viewHolder.is_select.setVisibility(View.VISIBLE);
        } else {
            viewHolder.is_select.setVisibility(View.GONE);
        }
        Glide.with(context).load(dataList.get(position).getGoods_pic()).into(viewHolder.good_logo);
//        x.image().bind(viewHolder.good_logo, dataList.get(position).getGoods_pic());
        viewHolder.text_title.setText(dataList.get(position).getGoods_name());
        viewHolder.text_price.setText("¥" + dataList.get(position).getGoods_price() + "/" + dataList.get(position).getGoods_unit());
        if (dataList.get(position).getGoods_unit() == null || dataList.get(position).getGoods_unit().equals("")) {
            viewHolder.text_price.setText("¥" + dataList.get(position).getGoods_price() + "/套");
        }


        viewHolder.shopping_number.setText("" + Double.parseDouble(dataList.get(position).getGoods_nums()));
        viewHolder.text_desc.setText(dataList.get(position).getSku_info());
        //
        viewHolder.ll_add.setTag(viewHolder.shopping_number);
        viewHolder.ll_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TextView textView = (TextView) v.getTag();
                Double nums = 1.0;
                try {
                    nums = Double.parseDouble(textView.getText().toString()) + 1.0;
                } catch (Exception w) {
                    nums = 1.0;
                }
                dataList.get(position).setGoods_nums(nums + "");
                //
                LookDetialNumsBean numsBean = new LookDetialNumsBean();
                numsBean.setNums(nums + "");
                numsBean.setPosition(position);
//                EventBus.getDefault().post(numsBean);

                BroadCastReceiverUtil.sendBroadcast(context, Constants.UPDATE_PRICE_ADD_KIT);
                textView.setText("" + nums);
                MyApplication.getInstance().setKitnums(Double.parseDouble(textView.getText().toString()));
                MyApplication.getInstance().setKitchenPackagePrice_GoodsPrice(dataList.get(position).getGoods_price());
                Log.e("商品价格", MyApplication.getInstance().getKitchenPackagePrice_GoodsPrice());
            }
        });
        viewHolder.reduce.setTag(viewHolder.shopping_number);
        viewHolder.reduce.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TextView textView = (TextView) v.getTag();
                Double nums = 1.0;
                try {
                    nums = Double.parseDouble(textView.getText().toString()) - 1.0;
                } catch (Exception w) {
                    nums = 1.0;
                }
                if (nums == 0.0) {
                    Toast.makeText(context, "再减就没了", Toast.LENGTH_SHORT).show();
                    nums = 1.0;
                }else {
                    dataList.get(position).setGoods_nums(nums + "");
                    LookDetialNumsBean numsBean = new LookDetialNumsBean();
                    numsBean.setNums(nums + "");
                    numsBean.setPosition(position);
//                EventBus.getDefault().post(numsBean);
                    MyApplication.getInstance().setKitchenPackagePrice_GoodsPrice(dataList.get(position).getGoods_price());
                    Log.e("商品价格", MyApplication.getInstance().getKitchenPackagePrice_GoodsPrice());

                    BroadCastReceiverUtil.sendBroadcast(context, Constants.UPDATE_PRICE_DEL_KIT);
                    textView.setText("" + nums);
                    MyApplication.getInstance().setKitnums(Double.parseDouble(textView.getText().toString()));
                }

            }
        });
        final AllocationViewHolder finalViewHolder = viewHolder;
        viewHolder.shopping_number.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final AlertDialog.Builder builder = new AlertDialog.Builder(context);
                View view = View.inflate(context, R.layout.dialog_edit, null);
                TextView queding1 = (TextView) view.findViewById(R.id.queding1);
                TextView quxaio1 = (TextView) view.findViewById(R.id.quxiao1);
                final EditText shopping_number = (EditText) view.findViewById(R.id.shopping_number);
                shopping_number.setText("" + Double.parseDouble(dataList.get(position).getGoods_nums()));
                MyApplication.getInstance().setKitchenPackagePrice_GoodsPrice(dataList.get(position).getGoods_price());
                MyApplication.getInstance().setKitnums(Double.parseDouble(shopping_number.getText().toString()));
                shopping_number.postInvalidate();
                //切换后将EditText光标置于末尾
                CharSequence charSequence = shopping_number.getText();
                if (charSequence instanceof Spannable) {
                    Spannable spanText = (Spannable) charSequence;
                    Selection.setSelection(spanText, charSequence.length());
                }

                builder.setView(view);
                final AlertDialog show = builder.show();
                Window dialogWindow = show.getWindow();
                WindowManager.LayoutParams lp = dialogWindow.getAttributes();
                dialogWindow.setGravity(Gravity.CENTER);
//                lp.width = MyApplication.getInstance().window_w / 3; // 宽度
                dialogWindow.setAttributes(lp);
                queding1.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (shopping_number.getText().toString().equals("")) {
                            Toast.makeText(context, "数量不能为空", Toast.LENGTH_SHORT).show();
                            return;
                        }

                        if (Double.parseDouble(shopping_number.getText().toString()) == 0.0) {
                            Toast.makeText(context, "数量不能小于0.0", Toast.LENGTH_SHORT).show();
                            return;
                        }

                        Double nums = Double.parseDouble(shopping_number.getText().toString());

                        dataList.get(position).setGoods_nums(nums + "");
                        LookDetialNumsBean numsBean = new LookDetialNumsBean();
                        numsBean.setNums(nums + "");
                        numsBean.setPosition(position);
//                        EventBus.getDefault().post(numsBean);
                        MyApplication.getInstance().setInputnums_kit(nums);
                        finalViewHolder.shopping_number.setText(nums + "");
                        BroadCastReceiverUtil.sendBroadcast(context, Constants.UPDATE_PRICE_INPUT_KIT);
                        show.dismiss();
                    }
                });
                quxaio1.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        show.dismiss();
                    }
                });

            }
        });
        return convertView;
    }

    class AllocationViewHolder {
        ImageView good_logo, img_toast, reduce, ll_add;
        TextView text_title, text_price, text_desc;
        TextView shopping_number;
        TextView text1, text2, text3;
        private LinearLayout is_select;
    }
}
